
const fourToTwo = function(val){
  let result = val ;
  if(val && val.indexOf('****')){
	  result = val.replace('****','**');
  }
  return result;
}

const timeChinese = function(value){
  let theTime = parseInt(value); // 秒
  let middle = 0; // 分
  let hour = 0; // 小时
  if(theTime <= 0) theTime = 0;
 
  if (theTime > 60) {
    middle = parseInt(theTime / 60);
    theTime = parseInt(theTime % 60);
    if (middle > 60) {
      hour = parseInt(middle / 60);
      middle = parseInt(middle % 60);
    }
  }
  let result = "" + parseInt(theTime) + "秒";
  if (middle > 0) {
    result = "" + parseInt(middle) + "分" + result;
  }
  if (hour > 0) {
    result = "" + parseInt(hour) + "小时" + result;
  }
  return result;
}

const numDouhaoZhengshu = function(n) {
	if(typeof n != 'number') return n;
	const regex = /\d{1,3}(?=(\d{3})+(\.|$))/g // 替换规则
	n = String(Math.round(n * Math.pow(10, 2))) // 乘100 四舍五入
	let integer = n.substr(0, n.length - 2).replace(regex, '$&,') // 最后两位前的为整数
	let decimal = n.substr(n.length - 2) // 最后两位为小数
	const result = `${integer || 0}`
	return result
}

const numDouhao = function(n) {
	const regex = /\d{1,3}(?=(\d{3})+(\.|$))/g // 替换规则
	n = String(Math.round(n * Math.pow(10, 2))) // 乘100 四舍五入
	let integer = n.substr(0, n.length - 2).replace(regex, '$&,') // 最后两位前的为整数
	let decimal = n.substr(n.length - 2) // 最后两位为小数
	const result = `${integer || 0}.${decimal}`
	return result
}

const numToK = function(val){
	if(typeof val != 'number'){
		return val;
	}
	if(val < 0 ) return 0;
	if(Math.abs(val) > 10000){
		return (val / 10000).toFixed(2) + 'w';
	}else if(Math.abs(val) > 1000){
		return (val / 1000).toFixed(2) + 'k';
	}
	return val;
}

const moneyTodouhao = function(val){
	if(Math.abs(val) > 10000){
		return (val / 10000).toFixed(2) + 'w';
	}
	return val;
}

const moneyShizhi = function(num) {
    if (num == null || num == "" || typeof(num) == undefined) {
        return 0;
    } else {
		if(num > 10000000){
			return (num / 100000000).toFixed(2) + '亿'
		}
		if(num > 10000){
			return (num / 10000).toFixed(2) + '万'
		}
        let tempNum = (num).toFixed(2);
        return Number(tempNum);
    }
}

const moneyFenToYuan = function(num) {
    if (num == null || num == "" || typeof(num) == undefined) {
        return 0;
    } else {
        let tempNum = (num/100).toFixed(2);
        return Number(tempNum);
    }
}

const numberFormat  = function(val) {
  if(!val) return 0;
  if(val < 10000 ) return val;
  return val = (val / 10000).toFixed(2) + 'w';
}

const chineseDay  = function(isDate) {
  if(!isDate) return '';
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day) + '   ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes);
  return s;
}

const monthDayHourMinutes  = function(isDate) {
  if(!isDate) return '';
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day) + '   ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes);
  return s;
}
const yearMonthDay  = function(isDate) {
  if(!isDate) return '';
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
  return s;
}

const MonthDay  = function(isDate) {
  if(!isDate) return '';
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day) + '   ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes);
  return s;
}

const hourMinutes = function(isDate) {
    if(!isDate) return '';
    var d = new Date(isDate);
    var year = d.getFullYear();
    var mon = d.getMonth() + 1;
    var day = d.getDate();
    let hours = d.getHours();
    let minutes = d.getMinutes();
    let seconds = d.getSeconds();
    let s;
    s = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes);
    return s;
}

const vts = function(value) {
    let tempObj = {
     'NO_UPLOAD': '未上传',
     'UPLOAD_ING':'上传中',
     'TRANSCODING': '转码中',
     'TRANS_OVER': '转码完成',
     'UPLOAD_ERROR': '上传失败',
     'TRANS_ERROR': '转码失败',
   }
   return tempObj[value] ? tempObj[value] : '未知';
}

const chineseYearMonth  = function(isDate) {
  if(!isDate) return '';
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
  return s;
} 

const chineseMonthDay  = function(isDate) {
  if(!isDate) return '';
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
  return s;
} 

const chineseMonthDayIndex  = function(isDate) {
  if(!isDate) isDate = new Date();
  var d = new Date(isDate);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds();
  let s;
  s = (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
  return s;
} 



export {
	chineseMonthDayIndex,
	chineseMonthDay,
	numToK,
	timeChinese,
	numDouhaoZhengshu,
	numDouhao,
	moneyTodouhao,
	chineseDay,
	MonthDay,
    monthDayHourMinutes,
	moneyShizhi,
	fourToTwo
}
